package com.aaa.controller.back;

import com.aaa.entity.*;
import com.aaa.service.back.AllotService;
import com.aaa.service.back.GoodsService;
import com.aaa.service.back.WarehouseService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/allot")
@Controller
public class AllotController {

    @Resource
    private AllotService allotService;
    @Resource
    private GoodsService goodsService;
    @Resource
    private WarehouseService warehouseService;

    /**
     * 调拨单页面显示
     * @return
     */
    @RequestMapping("/allotShow")
    public String allotShow(Model model){
        List<Map<String, Object>> allotStateList1 = allotService.findAllotByAllotState1();
        model.addAttribute("allotStateList1",allotStateList1);

        List<Map<String, Object>> allotStateList0 = allotService.findAllotByAllotState0();
        model.addAttribute("allotStateList0",allotStateList0);
        return "back/allot-list";
    }

    /**
     * 调拨单明细显示
     * @return
     */
    @RequestMapping("/allotDetailsShow")
    public String allotDetailsShow(Model model,String allotId){
        List<Map<String, Object>> allotDetailsList = allotService.findAllotDetails(allotId);
        model.addAttribute("allotDetailsList",allotDetailsList);
        return "back/allotDetails-list";
    }

    /**
     * 选择商品进行调拨单创建页面的显示
     * @param model
     * @return
     */
    @RequestMapping("/allotGoodsShow")
    public String allotGoodsShow(Model model){
        List<Map<String, Object>> goodsList = goodsService.findGoods();
        model.addAttribute("goodsList",goodsList);

        List<Map<String, Object>> warehouseList = warehouseService.findWarehouse();
        model.addAttribute("warehouseList",warehouseList);

        return "back/allotGoods-list";
    }

    /**
     * 更新session购物车商品
     * @param session
     */
    public void updateCart(HttpSession session) {
        int cartCount= 0;
        double cartAmount=0;
        Map<Integer, Goods> cart = (Map<Integer, Goods>)session.getAttribute("allot_cart");

        if (cart!=null) {
            for (Goods goods : cart.values()) {
                cartCount+=goods.getGoodsCount();
                cartAmount+=goods.getGoodsCount()*goods.getTradePrice();
            }

            session.setAttribute("cartCount", cartCount);
            session.setAttribute("cartAmount", cartAmount);
        }
    }

    /**
     * 添加商品到session购物车
     */
    @RequestMapping("/cartAdd")
    public String cartAdd(Integer gId,HttpSession session) {
        Map<Integer, Goods> cart = null;
        if (session.getAttribute("allot_cart")==null) {
            cart = new HashMap<Integer, Goods>();
            session.setAttribute("allot_cart", cart);
        } else {
            cart = (Map<Integer, Goods>)session.getAttribute("allot_cart");
        }

        Goods goods = cart.get(gId);

        if (goods==null) {
            goods = (Goods) goodsService.findGoodsByGoodsId(gId);
            goods.setGoodsCount(1);
            cart.put(gId, goods);
        } else {
            goods.setGoodsCount(goods.getGoodsCount()+1);
        }
        updateCart(session);
        return "redirect:/allot/allotGoodsShow";
    }

    /**
     * 修改session购物车的商品数量
     * @param gId
     * @param op
     * @param session
     * @return
     */
    @RequestMapping("/cart_process")
    public String cart_process(Integer gId,String op,HttpSession session){

        Map<Integer,Goods> cart = (Map<Integer,Goods>)session.getAttribute("allot_cart");

        if(cart!=null){
            Goods goods = cart.get(gId);
            if("add".equals(op)){
                goods.setGoodsCount(goods.getGoodsCount()+1);
            }else if("sub".equals(op)){
                //减数量时，如果商品只有1个，则直接移除
                if(goods.getGoodsCount()==1){
                    cart.remove(gId);
                }else{
                    goods.setGoodsCount(goods.getGoodsCount()-1);
                }
            }else if("delete".equals(op)){
                cart.remove(gId);
            }
            //更新购物车数量和金额
            updateCart(session);
        }
        return "redirect:/allot/allotGoodsShow";
    }

    /**
     * 进行调拨单创建
     * @return
     */
    @RequestMapping("/allotSubmit")
    public String allotSubmit(HttpSession session, Allot allot){
//        Emp emp = (Emp) session.getAttribute("empInfo");
//        Emp emp1 = new Emp();
//        emp1.setEmpId(emp.getEmpId());
//        purchase.setEmp(emp1);
        Date date = new Date();
        allot.setBusinessDate(date);

        allotService.addAllot(allot);

        String allotByMAX = allotService.findAllotByMAX();

        Map<Integer,Goods> cart = (Map<Integer,Goods>)session.getAttribute("allot_cart");
        AllotDetails allotDetails = new AllotDetails();
        allotDetails.setAllotId(allotByMAX);



        for (Goods goods : cart.values()){

            allotDetails.setGoodsId(goods.getGoodsId());
            allotDetails.setGoodsCount(goods.getGoodsCount());
            allotService.addAllotDetails(allotDetails);
        }
        session.removeAttribute("allot_cart");
        session.removeAttribute("cartAmount");

        return "redirect:/allot/allotShow";
    }

    /**
     * 修改调拨单未审核为已审核,并且生成调拨出库单
     * @return
     */
    @RequestMapping("/editAllotState1")
    public String editAllotState1(String allotId){

        allotService.editAllot1(allotId);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String strDate = sdf.format(new Date());
        String allotOutId = "DBCK"+strDate;

        Emp emp = new Emp();
        emp.setEmpId(2);
        AllotOut allotOut = new AllotOut();
        allotOut.setAllotOutId(allotOutId);
        allotOut.setEmpId(emp.getEmpId());
        allotOut.setBusinessDate(new Date());
        allotOut.setAllotId(allotId);

        List<Map<String, Object>> allotList = allotService.findAllotState0();
        for (Map<String,Object> map : allotList){
            Integer warehouseId = (Integer) map.get("warehouseId");
            allotOut.setWarehouseId(warehouseId);
        }
        allotService.addAllotOut(allotOut);

        List<Map<String, Object>> allotDetailsList = allotService.findAllotDetails(allotId);
        for (Map<String,Object> map : allotDetailsList){
            String goodsId = (String) map.get("goodsId");
            Integer goodsCount = (Integer) map.get("goodsCount");

            AllotOutDetails allotOutDetails = new AllotOutDetails();
            allotOutDetails.setAllotOutId(allotOutId);
            allotOutDetails.setGoodsId(goodsId);
            allotOutDetails.setGoodsCount(goodsCount);
            allotService.addAllotOutDetails(allotOutDetails);
        }

        return "redirect:/allot/allotShow";
    }

    /**
     * 查询调拨出库单
     * @return
     */
    @RequestMapping("/allotOutShow")
    public String allotOutShow(Model model){
        List<Map<String, Object>> allotOutList0 = allotService.findAllotOut0();
        model.addAttribute("allotOutList0",allotOutList0);

        List<Map<String, Object>> allotOutList1 = allotService.findAllotOut1();
        model.addAttribute("allotOutList1",allotOutList1);
        return "back/allotOut-list";
    }

    /**
     * 根据调拨出库单编号查询调拨出库单明细
     * @param model
     * @param allotOutId
     * @return
     */
    @RequestMapping("/allotOutDetailsShow")
    public String allotOutDetailsShow(Model model,String allotOutId){
        List<Map<String, Object>> allotOutDetailsList = allotService.findAllotOutDetails(allotOutId);
        model.addAttribute("allotOutDetailsList",allotOutDetailsList);
        return "back/allotOutDetails-list";
    }

    /**
     * 调拨出库减少库存数量,并且创建调拨入库单和调拨入库明细
     * @return
     */
    @RequestMapping("/editWarehouseDetailsReduce")
    public String editWarehouseDetailsReduce(AllotOut allotOut,String allotOutId,String allotId){

        allotService.editAllotOut(allotOut);

        AllotOut allotOut1 = allotService.findByAllotOutId(allotOutId);

        List<AllotOutDetails> allotOutDetailsList = allotService.findByAllotOutDetails(allotOutId);

        for (AllotOutDetails allotOutDetails : allotOutDetailsList){
            allotService.editWarehouseDetailsReduce(allotOutDetails.getGoodsCount(),allotOutDetails.getGoodsId(),allotOut1.getWarehouseId());
        }


        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String strDate = sdf.format(new Date());
        String allotEntryId = "DBRK"+strDate;

        Emp emp = new Emp();
        emp.setEmpId(2);

        AllotEntry allotEntry = new AllotEntry();
        allotEntry.setAllotEntryId(allotEntryId);
        allotEntry.setEmpId(emp.getEmpId());
        allotEntry.setBusinessDate(new Date());
        allotEntry.setAllotId(allotId);


        List<Map<String, Object>> allotList = allotService.findAllotState();
        for (Map<String,Object> map : allotList){
            Integer warehouseId = (Integer) map.get("warehouseId");
            allotEntry.setWarehouseId(warehouseId);
        }

        allotService.addAllotEntry(allotEntry);


        List<Map<String, Object>> allotDetailsList = allotService.findAllotDetails(allotId);
        for (Map<String,Object> map : allotDetailsList){
            String goodsId = (String) map.get("goodsId");
            Integer goodsCount = (Integer) map.get("goodsCount");

            AllotEntryDetails allotEntryDetails = new AllotEntryDetails();
            allotEntryDetails.setAllotEntryId(allotEntryId);
            allotEntryDetails.setGoodsId(goodsId);
            allotEntryDetails.setGoodsCount(goodsCount);
            allotService.addAllotEntryDetails(allotEntryDetails);
        }

        return "back/allotOutDetails-list";
    }

    /**
     * 调拨入库显示
     * @param model
     * @return
     */
    @RequestMapping("/allotEntryShow")
    public String allotEntryShow(Model model){
        List<Map<String, Object>> allotEntryList0 = allotService.findAllotEntry0();
        model.addAttribute("allotEntryList0",allotEntryList0);

        List<Map<String, Object>> allotEntryList1 = allotService.findAllotEntry1();
        model.addAttribute("allotEntryList1",allotEntryList1);
        return "back/allotEntry-list";
    }

    /**
     * 根据调拨入库单编号查询调拨入库单明细
     * @param model
     * @param allotEntryId
     * @return
     */
    @RequestMapping("/allotEntryDetailsShow")
    public String allotEntryDetailsShow(Model model,String allotEntryId){
        List<Map<String, Object>> allotEntryDetailsList = allotService.findAllotEntryDetails(allotEntryId);
        model.addAttribute("allotEntryDetailsList",allotEntryDetailsList);
        return "back/allotEntryDetails-list";
    }

    /**
     * 调拨入库增加库存数量
     * @return
     */
    @RequestMapping("/editWarehouseDetailsIncrease")
    public String editWarehouseDetailsIncrease(AllotEntry allotEntry,String allotEntryId,String allotId){

        allotService.editAllotEntry(allotEntry);

        AllotEntry allotEntry1 = allotService.findByAllotEntryId(allotEntryId);

        List<AllotEntryDetails> allotEntryDetailsList = allotService.findByAllotEntryDetails(allotEntryId);

        for (AllotEntryDetails allotEntryDetails : allotEntryDetailsList){
            allotService.editWarehouseDetailsIncrease(allotEntryDetails.getGoodsCount(),allotEntryDetails.getGoodsId(),allotEntry1.getWarehouseId());
        }

        return "back/allotEntry-list";
    }

}
